/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.util;

import com.thienloc.omap.entity.ServiceIntro;
import com.thienloc.omap.session.ServiceIntroFacade;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.IOException;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@RequestScoped
public class ImageServlet extends HttpServlet {
    
    private static final int DEFAULT_BUFFER_SIZE = 10240;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @EJB
    private ServiceIntroFacade facade;

    /**
     * Creates a new instance of ImageServlet
     */
    public ImageServlet() {
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        
        try {
            Long id = Long.parseLong(request.getParameter("id"));
            if (id == null) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
                return;
            }
            ServiceIntro serviceIntro = facade.find(id);
            // Init servlet response.
            response.reset();
            response.setBufferSize(DEFAULT_BUFFER_SIZE);
            response.setContentType("image/jpeg");
            response.setHeader("Content-Length", String.valueOf(serviceIntro.getImage().length));
            response.setHeader("Content-Disposition", "inline; filename=\"" + serviceIntro.getImage() + "\"");

            // Prepare streams.
            BufferedInputStream input = null;
            BufferedOutputStream output = null;
            
            try {
                // Open streams.
                input = new BufferedInputStream(new ByteArrayInputStream(serviceIntro.getImage()), DEFAULT_BUFFER_SIZE);
                output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

                // Write file contents to response.
                byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
                int length;
                while ((length = input.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                }
            } finally {
                // Gently close streams.
                close(output);
                close(input);
            }
        } catch (Exception ex) {
            logger.error("", ex);
        }
    }
    
    private void close(Closeable resource) {
        if (resource != null) {
            try {
                resource.close();
            } catch (Exception ex) {
                logger.error("", ex);
            }
        }
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
